<?php

require_once($cfgPaths['lib/dba'].'dbaCMS.o.inc.php');

class dbaCMSObject
{

var $mogrifiedExtensions = array('.gif','.png','.jpg');

 function dbaCMSObject(&$dba){
   $this->dba     = &$dba;
   $this->dbacms  = new dbaCMS($dba);
 }

 function add ($cat,$name,$alttext,$img,$img_mime,$img_width,$img_height,$size){
   $check = $this->dba->query("insert into ".$this->dba->tableforwarder."_cmsImg (name,alttext,cat) values("
      .$this->dba->genQuotedFieldStr(array($name, $alttext, $cat)).");");
   echo $this->dba->getError();
   if ($check){
     $insert_id = $this->dba->insert_id();
     $this->store_images($insert_id,$img,$img_mime,$img_width,$img_height,$size,$this->cfgPath,$this->cfgUrl);
     return true;
   } // if $check
   return $check;
 }

 function update ($id,$name,$alttext,$img,$img_mime,$img_width,$img_height,$size){
   $check = $this->dba->query("update ".$this->dba->tableforwarder."_cmsImg set "
        .$this->dba->genUpdateFieldStr(array('name'=>$name,'alttext'=>$alttext))." where id='$id';");
   echo $this->dba->getError();
   if ($check){
     $this->store_images($id,$img,$img_mime,$img_width,$img_height,$size,$this->cfgPath,$this->cfgUrl);
     return true;
   } // if $check
   return $check;
 }

 function updateCat ($id,$cat){
   $check = $this->dba->query("update ".$this->dba->tableforwarder."_cmsImg set cat='$cat' where id='$id';");
   echo $this->dba->getError();
   return $check;
 }

 function del ($id){
    $this->dba->query("select type from ".$this->dba->tableforwarder."_cmsImg where id=$id;");
    $result = $this->dba->fetch_row_array();
    $name = $this->cfgPath.$id.$this->dba->get_imgextension($result['type']);
    if (file_exists($name))
      syn_unlink($name);
    $this->dba->query("delete from ".$this->dba->tableforwarder."_cmsImg where id=$id;");
 }

 function get($id){
   $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_cmsImg where id='$id';");
   if ($sth){
     return $sth->fetch_row_Array();
   }
 }

 function getByCat($cat){
     return $this->getByCatWhere($cat);
 }

 function getByCatWhere($cat,$where=''){
   if ($where && $where != '()') $where = ' AND '.$where;
   $sth = $this->dba->query("select distinct E.* from ".$this->dba->tableforwarder."_cmsElement E,".$this->dba->tableforwarder."_cmsElementObjCat C where C.element_id=E.element_id and C.cat_id='$cat'$where;");
   if ($sth){
     return $sth->fetch_rows_Array();
   }
 }

 function getBySiteAndCatWhere($cat,$site,$where)
 {
   if ($where && $where != '()') $where = ' AND '.$where;
   $sth = $this->dba->query("select distinct E.* from ".$this->dba->tableforwarder."_cmsElement E,"
        .$this->dba->tableforwarder."_cmsElementInstance I,".$this->dba->tableforwarder."_cmsDoc D, ".$this->dba->tableforwarder."_cmsElementObjCat C where ".
        "D.id=doc_id and E.element_id=I.element_id and site_id=$site and E.element_id=C.element_id and cat_id=$cat{$where};");
   if ($sth){
     return $sth->fetch_rows_Array();
   }
 }
  
 function searchMetaBySite($searchquery,$site,$where)
 {
   if ($where && $where != '()') $where = ' AND '.$where;
   $sth = $this->dba->query("select E.* from ".$this->dba->tableforwarder."_cmsElement E,"
        .$this->dba->tableforwarder."_cmsElementInstance I ,".$this->dba->tableforwarder."_cmsDoc D where "
        ."(meta_name like '%$searchquery%' or meta_description like '%$searchquery%') and D.id=doc_id and E.element_id=I.element_id and site_id=$site{$where} group by element_id;");
   if ($sth){
     return $sth->fetch_rows_Array();
   }
 }
  
 function searchMeta($searchquery, $where)
 {
   if ($where && $where != '()') $where = ' AND '.$where;
   $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_cmsElement E where "
        ."(meta_name like '%$searchquery%' or meta_description like '%$searchquery%') $where group by element_id;");
   if ($sth){
     return $sth->fetch_rows_Array();
   }
 }
  
 function getElementInstances($el_id)
 {
   $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_cmsElementInstance where element_id='$el_id';");
   if ($sth){
     return $sth->fetch_rows_Array();
   }
 }
 
 function checkChilds ($cat)
 {
   $this->dba->query("select cat_id from ".$this->dba->tableforwarder."_cmsElementObjCat where cat_id='$cat';");
   if ($this->dba->num_rows()) return true;
 }
 
 function getDocObj ()
 {
 	return new elements($this->dbacms->getDocs());
 }

}

?>
